import {
	login,
	weLogin,
	loginOut,
	sendSmsCode,
	getCurrentUser,
	getStoreCountryCurrMoneyName,
	checkAccount
} from '@/api/http/user'
import storage from '@/util/storage'
import i18n from 'i18n'

const state = {
	userInfo: null,
	loginBack: false,
	isLogin: storage.check('token'),
	currency: '$',
	accountRepeat: false
}

const mutations = {
	SET_USER_INFO(state, data) {
		state.userInfo = data
		storage.set('userInfo', data)
	},
	SET_LOGIN_BACK(state, data) {
		state.loginBack = data
	},
	SET_LOGIN_STATE(state, data) {
		state.isLogin = storage.check('token')
	},
	SET_CURRENCY(state, data) {
		state.currency = data.countryCurrMoneySymbol || '$'
	},
	SET_REPEAT(state, data) {
		state.accountRepeat = data
	}
}

const actions = {
	// 登录
	Login({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			login(data).then(res => {
				if (res.code === 200) {
					commit('SET_USER_INFO', res.data)
					commit('SET_LOGIN_BACK', true)
					commit('SET_LOGIN_STATE')
					resolve()
					uni.navigateBack();
				} else {
					uni.showToast({
						icon: 'none',
						title: res.message
					});
				}
			}).catch(err => {
				reject(err)
			})
		})
	},

	// 微信登录
	weChatLogin({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			weLogin(data).then(res => {
				console.log(res)
				switch (res.code) {
					case 200:
						storage.set('token', res.data)
						commit('SET_LOGIN_BACK', true)
						commit('SET_LOGIN_STATE')
						resolve()
						// uni.navigateBack();
						// #ifdef H5
						uni.reLaunch({
							url: '/pages/index/home/index'
						})
						// #endif
						break;
					case 201:
						uni.showModal({
							content: '此微信号未绑定手机，是否前往绑定？',
							confirmColor: '#0049AC',
							success: e => {
								if (e.confirm) {
									uni.navigateTo({
									    url: '/pages/user/login/bindPhone?openId=' + res.data
									});
								}
							}
						})
						break
					default:
						uni.showToast({
							icon: 'none',
							title: res.message
						});
						break;
				}
			}).catch(err => {
				reject(err)
			})
		})
	},

	// 获取用户数据
	UserInfo({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			getCurrentUser().then(res => {
				if (res.code === 200) {
					commit('SET_USER_INFO', res.data)
					resolve(res.data)
				} else {
					commit('SET_USER_INFO', null)
					uni.showToast({
						icon: 'none',
						title: res.message
					})
				}
			}).catch(err => {
				reject(err)
			})
		})
	},

	// 退出登录
	LoginOut({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			uni.showModal({
				content: i18n.t('locales.isLoginOut'),
				confirmColor: '#0049AC',
				confirmText: i18n.t('locales.determine'),
				cancelText: i18n.t('locales.cancel'),
				success: success => {
					if (success.confirm) {
						loginOut().then(res => {
							if (res.code === 200) {
								storage.remove('token')
								commit('SET_LOGIN_STATE')
								resolve()
							} else {
								uni.showToast({
									icon: 'none',
									title: res.message
								});
							}
						}).catch(err => {
							reject(err)
						})
					}
				}
			})
		})
	},

	// 发送短信验证码
	SendSmsCode({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			sendSmsCode(data).then(res => {
				res.code === 200 && resolve()
				uni.showToast({
					icon: 'none',
					title: res.message
				});
			}).catch(err => {
				reject(err)
			})
		})
	},

	// 货币
	Currency({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			getStoreCountryCurrMoneyName({
				...data
			}).then(res => {
				if (res.code === 200) {
					resolve()
					commit('SET_CURRENCY', res.data)
				}
			}).catch(err => {
				reject(err)
			})
		})
	},

	CheckRegisterIn({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			if (!data.username) return
			// 手机号
			const phone = data.accountType === 2 && data.username
			// 邮箱
			const email = data.accountType === 1 && /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(data.username)
			if (phone || email) {
				checkAccount({
					...data
				}).then(res => {
					if (res.code === 200) {
						commit('SET_REPEAT', false)
						resolve(res.data)
					} else {
						commit('SET_REPEAT', true)
						uni.showToast({
							icon: 'none',
							title: res.message
						});
					}
				}).catch(err => {
					reject(err)
				})
			}
		})
	}
}
export default {
	state,
	mutations,
	actions
}
